/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import entities.Topic;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Thanh
 */
public class GetTopicsDAO {
    public ArrayList<Topic> getTopics() {
        ArrayList<Topic> results = new ArrayList<Topic>();
        try {
            String un = "sa";
            String pw = "1234";
            String url = "jdbc:sqlserver://localhost:1433;databaseName=BloggerDB;";
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = java.sql.DriverManager.getConnection(url, un, pw);

            String sql = "select t.TopicID, u.Username, u.FullName, t.Title, t.CreatedTime from Topics t left join Users u on t.UserID = u.UserID";
            PreparedStatement stm = con.prepareStatement(sql);
            ResultSet rs = stm.executeQuery();

            while (rs.next()) {
                Topic topic = new Topic();
                topic.setTopicID(rs.getInt("TopicID"));
                topic.setUsername(rs.getString("Username"));
                topic.setFullName(rs.getString("FullName"));
                topic.setTitle(rs.getString("Title"));
                topic.setCreatedTime(rs.getDate("CreatedTime").toString() + " " + rs.getTime("CreatedTime").toString());
                results.add(topic);
            }
            rs.close();
            stm.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return results;
    }
}
